using System;
using System.Web;

namespace SoftwareMonkeys.Scriptech.Scripts
{
	public class UrlMapper
	{
		public string MapUrl(string path, string queryStrings)
		{
			return path + "?" + queryStrings.TrimStart('?');
		}
		
		public string MapUrl(string path)
		{
			return MapUrl(HttpContext.Current.Request.ApplicationPath, 
			              path,
			              HttpContext.Current.Request.Url.Host,
			              HttpContext.Current.Request.Url.Port,
			              (HttpContext.Current.Request.Url.ToString().ToLower().IndexOf("https:") == 0));
		}
		
		public string MapUrl(string applicationPath, string relativeUrl, string host, int port, bool isSecure)
		{
			string protocol = (isSecure ? "https" : "http");
			
			if (port != 80)
				host = host + ":" + port.ToString();
			
			relativeUrl = applicationPath.TrimEnd('/') + "/" + relativeUrl.TrimStart('/');
			
			return string.Format("{0}://{1}/{2}", protocol, host, (relativeUrl).Trim('/'));
		}
	}
}

